package com.ecube.mqtt.service;

import com.ecube.mqtt.MqttUtils;
import com.ecube.mqtt.mysql.Query;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SwapSendService {


    public Boolean start(String env,
                String externalCode,
                String ordersn,
                String plateNo,
                String model,
                         String startSoc,
                         String endSoc   ) throws InterruptedException {
        //发送换电回复报文
        if(model.equals("云端")){
            MqttUtils.sendCEMQTT(env, externalCode, ordersn, plateNo);
        }else {
            ordersn = getRandom2(16);
        }

        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Thread.sleep(5000);
        ordersn = ordersn + "-" + getRandom2(5);
        Boolean ceResResult =MqttUtils.sendCEResponse(env, externalCode, ordersn, plateNo, model, dateFormat.format(date),startSoc,endSoc);
        if(ceResResult){
            System.out.println("换电成功");
        }else {
            System.out.println("换电失败");
        }
        return ceResResult;
    }

    public Boolean tcustart(String env,
                         String externalCode,
                         String ordersn,
                         String plateNo,
                         String model,
                         String time,
                            String startSoc,
                            String endSoc) throws InterruptedException {

        //发送换电回复报文
//        if(model.equals("云端")){
//            MqttUtils.sendCEMQTT(env, externalCode, ordersn, plateNo);
//        }else {
//            ordersn = getRandom2(16);
//        }
        String swapSn = Query.getSwapSn(env, externalCode);
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //Thread.sleep(1000);
        //Boolean ceResResult =MqttUtils.sendCEResponse(env, externalCode, ordersn, plateNo, model, dateFormat.format(date));

        Thread.sleep(1000);
        String ordersn1 = getRandom2(16);
        Boolean ceEventResult =MqttUtils.sendCEChargeEventResponse(env, externalCode, swapSn, ordersn1, plateNo, dateFormat.format(date), time,startSoc,endSoc);

        if(ceEventResult && ceEventResult){
            System.out.println("换电成功");
        }else {
            System.out.println("换电失败");
        }
        return ceEventResult;
    }
    public Boolean startfail(String env,
                         String externalCode,
                         String ordersn,
                         String plateNo,
                         String result,
                         String reson) throws InterruptedException {

        //发送换电回复报文
        Boolean ceResult = MqttUtils.sendCEFailMQTT(env, externalCode, ordersn, plateNo, result, reson);

        if(ceResult){
            System.out.println("启动发送成功");
        }else {
            System.out.println("启动发送失败");
        }
        return ceResult;
    }

    public Boolean rate(String env,
                             String externalCode
                            ) throws InterruptedException {

        //发送换电回复报文
        Boolean rateResult = MqttUtils.sendModelMQTT(env, externalCode);

        if(rateResult){
            System.out.println("费率下发成功");
        }else {
            System.out.println("费率下发失败");
        }
        return rateResult;
    }

    public static String getRandom2(int len) {
        Random r = new Random();
        StringBuilder rs = new StringBuilder();
        for (int i = 0; i < len; i++) {
            rs.append(r.nextInt(10));
        }
        return rs.toString();
    }

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 1; i++) {
            System.out.println(i);
            sendMQ();
        }
  }

  public static void sendMQ() throws InterruptedException {
      Date date = new Date();
      SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String ordersn = getRandom2(16);
      MqttUtils.sendCEResponse("FAT", "111199", ordersn,
              "豫DD00000", "站控", dateFormat.format(date),"58","100");
//      Thread.sleep(4000);
//      String swapSn = Query.getSwapSn("FAT", "111199");
//      String ordersn1 = getRandom2(16);
//      MqttUtils.sendCEChargeEventResponse("FAT",
//              "111199",
//              swapSn,
//              ordersn1,
//              "豫DD00000",
//              dateFormat.format(date),
//              "200",
//              "60",
//              "100");
//
//
      }
}
